//
//  DKShortVideoListController.m
//  DKShortVideo
//
//  Created by Keer_LGQ on 2018/3/30.
//  Copyright © 2018年 DK. All rights reserved.
//

#import "DKShortVideoListController.h"
#import "DKShortVideoListCell.h"
#define LIST_VIDEO_CELL_ID @"LIST_VIDEO_CELL_ID"

@interface DKShortVideoListController ()
@property NSMutableArray *dataSourceArray;
@end

@implementation DKShortVideoListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"短视频";
    
    
    [self.tableView registerClass:[DKShortVideoListCell class] forCellReuseIdentifier:LIST_VIDEO_CELL_ID];
    _dataSourceArray = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {
        ListVideoModel *model = [ListVideoModel new];
        model.cover = @"https://upload-images.jianshu.io/upload_images/80684-ed25e9d115ddcb45..jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
        model.duration = 30;
        model.videoUrl = @"http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/gear2/prog_index.m3u8";
        model.title = @"测试";
        
        [_dataSourceArray addObject:model];
    }

}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; {
    return 130;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return _dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSInteger row = indexPath.row;
    ListVideoModel *param = [_dataSourceArray objectAtIndex:row];
    if (param) {
        DKShortVideoListCell *cell = [tableView dequeueReusableCellWithIdentifier:LIST_VIDEO_CELL_ID];
        [cell setDataSource:param];
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DKShortVideoListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell) {
        [cell previewAction];
    }
}

//video slide out off screen then stop render
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    DKShortVideoListCell *videoCell = (DKShortVideoListCell *)cell;
    if (videoCell) {
        [videoCell stopPreview];
    }
}

//video slide out on screen begin render
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    DKShortVideoListCell *videoCell = (DKShortVideoListCell *)cell;
    if (videoCell) {
        [videoCell stopPreview];
    }

}
@end
